package core.Resource
{
	import flash.errors.IOError;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;

	public class MapDefinition extends EventDispatcher
	{
		
		public static const MAP_DEF_LOAD_COMPLETE:String = "TGP:MapDefLoadComplete";
		
		//!!下面这些属性，除了mapId,应该从xml或swf中获取
		public var mapId:int = 9999;

		public var width:int = 2100;
		public var height:int = 1200;

		

		public var colsNum:int;
		public var rowsNum:int;

		

		public var colWidth:int = 300;
		public var rowHeight:int = 300;
		
		public var viewWidth:int = 1000;
		public var viewHeight:int = 700;
		public var viewColsNum:int;
		public var viewRowsNum:int;
		

		private var _loader:URLLoader;
		private var _xml:XML;
		
		public function MapDefinition()
		{
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE,completeHandler);
			_loader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
			init();
		}
		

		
		public function init():void
		{
			colsNum = width/colWidth;
			if(width%colWidth!=0)
				colsNum++;
			rowsNum = height/rowHeight;
			if(height%rowHeight!=0)
				rowsNum++;
			viewColsNum = viewWidth/colWidth+1;
			viewRowsNum = viewHeight/rowHeight+1;
		}
		

		public function get xml():XML
		{
			return _xml;
		}
		
		public function load(name:String,url:String):void
		{
			_loader.dataFormat = URLLoaderDataFormat.BINARY; 
			_loader.load(new URLRequest(url));	
		}
		
		private function completeHandler(e:Event):void
		{
			_xml = XML(URLLoader(e.target).data);
			this.dispatchEvent(new Event(MAP_DEF_LOAD_COMPLETE));
		}
		
		private function ioErrorHandler(e:IOError):void
		{
			throw new Error("TGP:MAP definition load failed.Reason:"+e.message);
		}
		
	}
}